<?php
$json = array();
if (count($rows) > 0) {
	$hidden = false;//loading from merchant account
	$anonymous_lbl = BUtils::T('anonymous_lbl');
	
	foreach ($rows as $r) {
		$visit_timeago = Yii::app()->format->timeago((int)$r['ts_last_time']);
		$visit_time = Yii::app()->locale->dateFormatter->formatDateTime($r['ts_last_time'], null, 'short');
		$visit_info = BUtils::T('last_visited_lbl', array('{time_ago}'=>$visit_timeago))  . ' ('.$visit_time.')';
		if (empty($r['id_user'])) {
			$cust_name = $anonymous_lbl;
			$cust_url = Yii::app()->baseUrl . '/img/user-128.png';
			$cust_contact = '';
		} else {
			//set user name
			if (!empty($r['name'])) $cust_name = substr($r['name'], 0, strpos($r['name'], ' '));
			else $cust_name = $anonymous_lbl;

			//set active phone number/email
			$cust_contact = '<ul class="icons-ul">';
			if (!empty($r['mobile'])) {
				$cust_contact .= '<li><i class="icon-li icon-phone"></i> ' . CustomerHelper::getMobile($r['mobile'], $hidden) . '</li>';
			}
			if(!empty($r['email'])) {
				$cust_contact .= '<li><i class="icon-li icon-envelope-alt"></i> ' . CustomerHelper::getEmail($r['email'], $hidden) . '</li>';
			}
			$cust_contact .= '</ul>';

			//set user picture
			if (empty($r['picture'])) $cust_url = Yii::app()->baseUrl . '/img/user-128.png';
			else $cust_url = $r['picture'];
		}

		//amount + points + redeemed points
		$points = Yii::app()->locale->numberFormatter->formatDecimal($r['point']);
		
		$json['customers'][] = array(
			'id' => $r['id_user'],
			'img' => $cust_url,
			'name' => $cust_name,
			'contact' => $cust_contact,
			'hitted' => empty($r['mobile']) ? $r['email'] : $r['mobile'],//always use mobile as hitted
			'last_time' => $visit_info,
			'visit_times' => $r['visit_times'],
			'points' => $points,
			'mobile' => $r['mobile'],
			'email' => $r['email'],
		);
	}
}
echo json_encode($json);